package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.ContractDao;
import cn.itcast.dao.cargo.ExtCproductDao;
import cn.itcast.domain.cargo.Contract;
import cn.itcast.domain.cargo.ExtCproduct;
import cn.itcast.domain.cargo.ExtCproductExample;
import cn.itcast.service.cargo.ExtCproductService;
import com.alibaba.druid.filter.AutoLoad;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;

@Service
public class ExtCproductServiceImpl implements ExtCproductService {


    @Autowired
    private ExtCproductDao extCproductDao; //附件dao

    @Autowired
    private ContractDao contractDao; //合同dao

    /**
     * 保存附件
     *  contract（购销合同）字段
     *     * proNum：货物数量
     *     * totalAmount：合同的总金额
     *  extCproduct（附件）字段
     *     * cnumber：数量
     *     * price：单价
     *     * amout：总金额
     *     * contractProductId: 附件所属的货物ID
     *     * contractId: 附件所属合同id
     */
    public void save(ExtCproduct ext) {
        //1、设置id
        ext.setId(UUID.randomUUID().toString());
        //2、计算附件总金额
        Double money = 0.0;
        if (ext.getCnumber() != null && ext.getPrice() != null) {
            money = ext.getCnumber() * ext.getPrice();
        }
        ext.setAmount(money);
        //3、保存附件
        extCproductDao.insertSelective(ext);
        //4、根据id查询合同
        Contract contract = contractDao.selectByPrimaryKey(ext.getContractId());
        //5、设置总金额和附件数
        contract.setExtNum(contract.getExtNum() + 1);
        contract.setTotalAmount(contract.getTotalAmount() + money);
        //6、更新合同
        contractDao.updateByPrimaryKeySelective(contract);
    }


    /**
     * 更新
     *  contract（购销合同）字段
     *    * proNum：货物数量
     *    * totalAmount：合同的总金额
     *  extCproduct（附件）字段
     *    * cnumber：数量
     *    * price：单价
     *    * amout：总金额
     *    * contractProductId: 附件所属的货物ID
     *    * contractId: 附件所属合同id
     */
    public void update(ExtCproduct newExt) {
        //1、计算修改之后的总金额
        Double newMoney = 0.0;
        if (newExt.getCnumber() != null && newExt.getPrice() != null) {
            newMoney = newExt.getCnumber() * newExt.getPrice();
        }
        newExt.setAmount(newMoney);
        //2、查询修改之前的总金额
        ExtCproduct oldExt = extCproductDao.selectByPrimaryKey(newExt.getId());
        Double oldMoney = oldExt.getAmount();
        //3、修改附件
        extCproductDao.updateByPrimaryKeySelective(newExt);
        //4、根据合同id，查询购销合同
        Contract contract = contractDao.selectByPrimaryKey(newExt.getContractId());
        //5、修改合同总金额
        contract.setTotalAmount(contract.getTotalAmount() - oldMoney + newMoney);
        //6、更新合同
        contractDao.updateByPrimaryKeySelective(contract);
    }


    /**
     * 删除
     */
    public void delete(String id) {
        //1、根据附件id，查询附件 
        ExtCproduct ext = extCproductDao.selectByPrimaryKey(id);
        Double money = ext.getAmount();
        //2、根据id删除附件
        extCproductDao.deleteByPrimaryKey(id);
        //3、根据合同id，查询合同
        Contract contract = contractDao.selectByPrimaryKey(ext.getContractId());
        //4、设置附件数和总金额
        contract.setExtNum(contract.getExtNum() -1);
        contract.setTotalAmount(contract.getTotalAmount() - money);
        //5、更新合同
        contractDao.updateByPrimaryKeySelective(contract);
    }

    @Override
    public ExtCproduct findById(String id) {
        return extCproductDao.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo findAll(ExtCproductExample example, int page, int size) {
        PageHelper.startPage(page,size);
        List<ExtCproduct> list = extCproductDao.selectByExample(example);
        return new PageInfo(list);
    }
    //
}
